<template>
  <div class="app-container project-communication-container">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="communicationList" height="400" :row-style="{ height: '30px' }" border
          :cell-style="{ padding: '0px' }">
          <el-table-column label="序号" type="index" :index="indexMethod" />
          <el-table-column label="合同编号" align="center" prop="contractNo" :show-overflow-tooltip="true" />
          <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true" />
          <el-table-column label="付款银行" align="center" prop="payingBank" :show-overflow-tooltip="true" />
          <el-table-column label="付款账号" align="center" prop="paymentAccount" :show-overflow-tooltip="true" />
          <el-table-column label="款项" align="center" prop="payment" :show-overflow-tooltip="true" />
          <el-table-column label="付款金额" align="center" prop="paymentMoney" :show-overflow-tooltip="true" />
          <el-table-column label="付款时间" align="center" prop="paymentTime" width="110" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="收款银行" align="center" prop="receivingBank" :show-overflow-tooltip="true" />
          <el-table-column label="收款账号" align="center" prop="receivingAccount" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="status" />
          <el-table-column label="确认人" align="center" prop="confirmName" :show-overflow-tooltip="true" />
          <el-table-column label="备注" align="center" prop="notes" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, scope.$index)"
                v-if="scope.row.status != '完结'" title="编辑"></el-button>
              <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleConfirm(scope.row)"
                v-if="scope.row.status != '完结'" title="确认"></el-button>
              <el-button size="mini" type="text" icon="el-icon-view"
                @click="handleSelect(scope.row, scope.$index)" title="查看"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改往来信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="cancel()">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <div v-if="addSign !== 0">
          <el-descriptions title="">
            <el-descriptions-item label="序号">
              <el-tag size="medium">{{ form.index }}</el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contractNo">
              <el-input v-model="form.contractNo" placeholder="请输入合同编号" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="付款银行" prop="payingBank">
              <el-input v-model="form.payingBank" placeholder="请输入付款银行" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="付款账号" prop="paymentAccount">
              <el-input v-model="form.paymentAccount" placeholder="请输入付款账号" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="款项" prop="payment">
              <el-input v-model="form.payment" placeholder="请输入款项" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款金额" prop="paymentMoney">
              <el-input v-model="form.paymentMoney" placeholder="请输入付款金额" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收款银行" prop="receivingBank">
              <el-input v-model="form.receivingBank" placeholder="请输入收款银行" :disabled="ReadOnly" />
            </el-form-item>
<!--            @focus="enterprise"-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款账号" prop="receivingAccount">
              <el-input v-model="form.receivingAccount" placeholder="请输入收款账号" :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="付款时间" prop="paymentTime">
              <el-date-picker clearable v-model="form.paymentTime" :disabled="ReadOnly" type="date"
                value-format="yyyy-MM-dd" placeholder="请选择付款时间 " style="width: 185px">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="notes">
              <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" style="width: 465px"
                :disabled="ReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="align-items: center;justify-content: center;display: flex;">
        <el-button type="primary" @click="submitForm" v-if="this.addSign != 2">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog title="选择企业" :visible.sync="openUser" width="800px" append-to-body>
      <supplier v-if="openUser" @select-enterprise="handleSelectEnterprise">
      </supplier>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunication, getCommunication, confirmCommunication, addCommunication, updateCommunication } from "@/api/communication/communication";
import MainType from "../../../components/MainType/index.vue";
import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Supplier from "./supplier.vue";
export default {
  name: "Communication",
  components: { Supplier, MainType, Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      openUser: false,
      typeCode: 'communication',
      // 往来信息表格数据
      communicationList: [],
      // 弹出层标题
      title: "",
      //是否禁用
      ReadOnly: false,
      //主分类
      options: null,
      addSign: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        projectName: null,
        classificationId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classificationId: [
          { required: true, message: '请选择主分类', trigger: 'change' },
        ],
        contractNo: [
          { required: true, message: '请输入合同编号', trigger: 'blur' },
        ],
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
        ],
        payingBank: [
          { required: true, message: '请输入付款银行', trigger: 'blur' },
        ],
        paymentAccount: [
          { required: true, message: '请输入付款账号', trigger: 'blur' },
        ],
        payment: [
          { required: true, message: '请输入款项', trigger: 'blur' },
        ],
        paymentMoney: [
          { required: true, message: '请输入付款金额', trigger: 'blur' },
        ],
        paymentTime: [
          { required: true, message: '请选择付款时间', trigger: 'change' },
        ],
        notes: [
          { required: true, message: '请输入备注', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.listType();
  },
  methods: {
    /** 查询往来信息列表 */
    getList() {
      this.loading = true;
      listCommunication(this.queryParams).then(response => {
        this.communicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.ReadOnly = false;
      this.reset();
    },
    //获取树节点数据
    listType() {
      listType({ typeCode: 'communication' }).then(res => {
        console.log(this.options)
        this.options = res.data;
      })
    },
    //序号展示
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    //树点击事件
    getNode(data) {
      console.log(data)
      if (data.parentId !== '00000000-0000-0000-0000-000000000000') {
        this.queryParams.classificationId = data.id
      } else {
        this.queryParams.classificationId = null;
      }
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNo: null,
        projectName: null,
        payingBank: null,
        paymentAccount: null,
        payment: null,
        paymentMoney: null,
        paymentTime: null,
        receivingBank: null,
        receivingAccount: null,
        status: null,
        confirmId: null,
        confirmName: null,
        notes: null,
        operateId: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationTime: null,
        operateTime: null,
        createId: null,
        classificationId: null,
        belongTo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    enterprise() {
      this.openUser = true;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.listType();
      this.open = true;
      this.addSign = 0;
      this.form.classificationId = '650c44e1-b575-4b62-9d54-1ba9d1e8abb7'
      this.title = "添加往来信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {
      this.reset();
      const id = row.id || this.ids
      this.listType();
      getCommunication(id).then(response => {
        this.form = response.data;
        this.addSign = 1;
        this.form.index = index + 1;
        this.open = true;
        this.title = "修改往来信息";
      });
    },
    //查看按钮
    handleSelect(row, index) {
      this.reset();
      const id = row.id || this.ids
      this.listType();
      getCommunication(id).then(response => {
        this.form = response.data;
        this.addSign = 2;
        this.form.index = index + 1;
        this.ReadOnly = true;
        this.open = true;
        this.title = "修改往来信息";
      });
    },
    //确认按钮
    handleConfirm(row) {
      this.$confirm('是否确认该数据为完结状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        confirmCommunication(row.id).then(res => {
          this.$message.success("确认成功");
          this.getList();
        })
      });
    },
    handleSelectEnterprise(data) {
      console.log(data.bankName)
      console.log(data.bankNo)
      // 处理企业选择
      this.form.receivingBank = data.bankName;
      this.form.receivingAccount = data.bankNo;
      this.openUser = false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommunication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommunication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

<style scoped lang="scss">
.project-communication-container {
  overflow-y: auto;
  overflow-x: auto;
  padding: 16px;
  max-width: 100%;
  max-height: 90vh;
  box-sizing: border-box;
}

// ... existing styles ...
</style>
